package com.example.service.Impl;

import com.example.common.exception.BaseException;
import com.example.entity.dto.ProjectDto;
import com.example.entity.dto.Sub_ProjectsDto;
import com.example.entity.vo.*;
import com.example.mapper.ProjectMapper;
import com.example.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    public void AddProject(ProjectDto projectDto) {
        projectMapper.AddProject(projectDto);
    }
    public void Add_subProject(Sub_ProjectsDto sub_projectDto) {
        projectMapper.Add_subProject(sub_projectDto);
    }
    public void DeleteProject(Integer Id) {
        projectMapper.DeleteProject(Id);
    }
    public void Delete_subProject(Integer Id) {
        projectMapper.Delete_subProject(Id);
        projectMapper.deleteSubDate(Id);
    }
    public Sub_ProjectsVo collect_sub(int id,String date){
        Sub_ProjectsVo sub_projectsVo = projectMapper.collect_sub(id,date);
        if (sub_projectsVo == null) {
            return new Sub_ProjectsVo();
        }else{
            Sub_ProjectsDto sub_projectsDto = new Sub_ProjectsDto();
            sub_projectsDto.setWorkTime(sub_projectsVo.getWorktime());
            sub_projectsDto.setOverTime(sub_projectsVo.getOvertime());
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date date1  = dateFormat.parse(date + "-02");
                sub_projectsDto.setTime(date1);
            }catch (Exception e){
                throw new BaseException("日期格式错误" + e);
            }
            getSub_ProjectsVo subProjectsVo = projectMapper.getSubByIdAndTime(id,date);
            if(subProjectsVo == null){
                sub_projectsDto.setProjectId(id);
                projectMapper.Add_subProjectDate(sub_projectsDto);
            }else{
                projectMapper.updatesubProjectDate(sub_projectsDto);
            }
        }
        return sub_projectsVo;
    }

    public ProjectsVo collect(Integer Id,String time){
        List<getSub_ProjectsVo> subProjectsDtoList = projectMapper.Get_subProject(Id);
        for (getSub_ProjectsVo getSub_projectsVo : subProjectsDtoList) {
            collect_sub(getSub_projectsVo.getId(), time);
        }
        return projectMapper.collect(Id,time);
    }
    public List<getProjectsVo> getProjectbyId(Integer Id){
        return projectMapper.getProjectbyId(Id);
    }
    public List<getProjectsVo> getProject(){
        return projectMapper.getProject();
    }

    public List<getSub_ProjectsVo> Get_subProject(int id) {
        return projectMapper.Get_subProject(id);
    }


    public List<getSub_ProjectsVo> get_subProjectbyId(Integer Id){
        return projectMapper.get_subProjectbyId(Id);
    }

    public void updateProject(ProjectDto projectDto){
        projectMapper.updateProject(projectDto);
    }

    public void updatesubProject(Sub_ProjectsDto sub_ProjectsDto){
        projectMapper.updatesubProject(sub_ProjectsDto);
    }

    /**
     * 获取分项目信息
     *
     * @param id
     * @param date
     * @return
     */
    public List<SubProjectsVo> getSubDetail(int id, String date) {
        return projectMapper.getSubDetail(id,date);
    }

    /**
     * 获取所有分项目
     * @return
     */
    public List<Sub_ProjectsDto> getSub() {
        return projectMapper.getSub();
    }

}
